package saucaca.Service;

import java.util.ArrayList;
import java.util.Random;

import saucaca.Object.SongObject;


public class PlaylistManager {

	public enum RepeatMode {
		NoRepeat, RepeatAll, RepeatOne
	}

	private RepeatMode mRepeatMode = RepeatMode.NoRepeat;
	private boolean mShuffle = false;

	private ArrayList<SongObject> mItems = null;
	private SongObject currentSong = null;
	public int currentIndex = -1;
	public int currentDuration=-1;
	
	public PlaylistManager() {
		mItems = new ArrayList<SongObject>();
	}

	public PlaylistManager(ArrayList<SongObject> list) {
		mItems = list;
	}

	public void setArrayList(ArrayList<SongObject> listSongs) {
		mItems = listSongs;
	}

	public ArrayList<SongObject> getArrayList() {
		return mItems;
	}

	public void add(SongObject si) {
		mItems.add(si);
	}
	public void remove(SongObject si) {
		if (si!=currentSong) {
			try {
				mItems.remove(si);
			} catch (Exception e) {
			}
		}
	}
	public SongObject gotoNextSong() {
		if (mItems.size() == 0) {
			return null;
		}

		if (mRepeatMode == RepeatMode.RepeatOne && currentSong != null)
			return currentSong;

		if (mShuffle) {
			Random rd = new Random();
			currentIndex = rd.nextInt(mItems.size());
		} else {
			if (currentIndex == -1) {
				currentIndex = 0;
			} else {
				currentIndex++;
				if (currentIndex == mItems.size() && mRepeatMode == RepeatMode.RepeatAll) {
					currentIndex = 0;
				} else {

				}
			}
		}

		if (currentIndex >= 0 && currentIndex < mItems.size())
			currentSong = mItems.get(currentIndex);
		else {
			currentIndex = -1;
			currentSong = null;
		}
		Singleton.getInstance().index = currentIndex;
		return currentSong;
	}

	public SongObject gotoPreviousSong() {
		if (mItems.size() == 0) {
			return null;
		}
		if (mRepeatMode == RepeatMode.RepeatOne && currentSong != null)
			return currentSong;

		if (mShuffle) {
			Random rd = new Random();
			currentIndex = rd.nextInt(mItems.size());
		} else {
			if (currentIndex == -1) {
				currentIndex = 0;
			} else {
				currentIndex--;
				if (currentIndex == -1 && mRepeatMode == RepeatMode.RepeatAll)
					currentIndex = mItems.size() - 1;
			}
		}

		if (currentIndex >= 0 && currentIndex < mItems.size())
			currentSong = mItems.get(currentIndex);
		else {
			currentIndex = -1;
			currentSong = null;
		}
		Singleton.getInstance().index = currentIndex;
		return currentSong;
	}

	public boolean setCurrentSong(SongObject si) {
		if (si == null) { // means stop
			currentIndex = -1;
			currentSong = null;
			return true;
		}

		int i;
		int index = -1;
		for (i = 0; i < mItems.size(); i++) {
			if (si.getSONG_ID() == mItems.get(i).getSONG_ID()) {
				index = i;
				break;
			}
		}
		if (index >= 0) {
			currentIndex = index;
			currentSong = mItems.get(currentIndex);
		}
		return (index >= 0);
	}

	public boolean setCurrentSongIndex(int index) {
		if (index < 0 || index >= mItems.size())
			return false;
		currentIndex = index;
		currentSong = mItems.get(currentIndex);
		return true;
	}

	public SongObject getCurrentSong() {
		return currentSong;
	}

	public int getCurrentSongIndex() {
		return currentIndex;
	}

	public void setRepeatMode(RepeatMode rp) {
		mRepeatMode = rp;
	}

	public RepeatMode getRepeatMode() {
		return mRepeatMode;
	}

	public void setShuffle(boolean s) {
		mShuffle = s;
	}

	public boolean isShuffle() {
		return mShuffle;
	}
}